#coding=utf-8
#!/usr/bin/python 
import os
import os.path

def findKeyWord(fullFileName,keyword):
	"统计文件的行数"
	totleLineCount = 0
	
	lines = []
	try:
	    f = open(fullFileName, 'rU')
	    lines = f.readlines()
	except IOError as e:
	    print e
	else:
	    for line in lines:
	        totleLineCount=totleLineCount+1
	        if line.find(keyword)!=-1:
	        	print "文件:%r  代码行:%r 内容" % (fullFileName,totleLineCount) + line
	        
	finally:
	    f.close()

def fileLineCount(fullFileName):
	"统计文件的行数"
	totleLineCount = 0
	spaceLineCount = 0
	usefulLineCount = 0
	commentLineCount = 0

	commentBeginLine = -1
	commentEndLine = -1
	lineCount = 0
	
	lines = []
	try:
		# http://www.zhihu.com/question/19751023 使用rU读写，统一将换成/n
	    f = open(fullFileName, 'rU')
	    lines = f.readlines()
	except IOError as e:
	    print e
	else:
	    for line in lines:
	        # print line
	        totleLineCount=totleLineCount+1

	        newline = line.strip()
	        lineLen = len(newline)
	        # print newline
	        # 计算空行
	        if  lineLen == 0 :
	        	spaceLineCount = spaceLineCount+1

	        # 计算注释行
	        if lineLen >= 2:
		        prefix = newline[0:2]
		        subfix = newline[len(newline)-2:len(newline)]

		        
		        prefixResult =cmp(prefix,"/*")
		        subfixResult =cmp(subfix,"*/")
		        if cmp(prefix,"//")==0:
		        	commentLineCount=commentLineCount+1

		        if cmp(prefix,"/*")==0:
		        	commentBeginLine=totleLineCount;
		        	# print "totleLineCount:%r prefix:%r subfix:%r  前缀cmp:%r" % (totleLineCount,prefix,subfix,prefixResult)
		        if cmp(subfix,"*/")==0:
		        	commentEndLine=totleLineCount;
		        	# print "totleLineCount:%r prefix:%r subfix:%r  后缀cmp:%r" % (totleLineCount,prefix,subfix,subfixResult)
		        
		        if (commentEndLine!=-1 and commentBeginLine!=-1):
		        	commentLineCount=commentEndLine-commentBeginLine+1+commentLineCount
		        	commentEndLine=-1
		        	commentBeginLine=-1
		
	finally:
	    f.close()

	usefulCount = totleLineCount-spaceLineCount-commentLineCount
	print "文件名:%r 总行数:%r  空行:%r 注释行:%r 可用代码行数:%r" % (fullFileName,totleLineCount,spaceLineCount,commentLineCount,usefulCount)
	return usefulCount


rootdir = "/Users/wesleyxiao/Documents/SVN/iPhoneQQ_6.0.0_VAS_PersonalResource/Classes/engine/QQ/ValueAddedService"

allLineCount = 0
fileNumCount = 0
for parent,dirnames,filenames in os.walk(rootdir):    #三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
	for filename in filenames:                        #输出文件信息
		fullFileName=os.path.join(parent,filename)
		findKeyWord(fullFileName,"QQ_TRACE")
		# fileNumCount=fileNumCount+1
		# allLineCount= allLineCount+ fileLineCount(fullFileName)


# print "文件数:%r 总行数:%r" % (fileNumCount,allLineCount)
	




		# print fullFileName